package com.saris.primego.web.controller.admin;

import com.saris.primego.common.result.PageResult;
import com.saris.primego.common.result.Result;
import com.saris.primego.pojo.dto.DishDTO;
import com.saris.primego.pojo.dto.DishPageQueryDTO;
import com.saris.primego.pojo.entity.Dish;
import com.saris.primego.pojo.vo.DishVO;
import com.saris.primego.service.DishService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * 菜品管理
 */
@RestController("adminDishController")
@RequestMapping("/admin/dish")
@Tag(name = "菜品管理")
@Slf4j
public class DishController {

    final DishService dishService;
    final RedisTemplate<String, Object> redisTemplate;

    public DishController(DishService dishService, RedisTemplate<String, Object> redisTemplate) {
        this.dishService = dishService;
        this.redisTemplate = redisTemplate;
    }

    @PostMapping
    @Operation(summary = "新增菜品", method = "POST")
    public Result<Dish> addDish(@RequestBody DishDTO dto) {
        dishService.addDishWithFlavor(dto);

        //清理缓存数据
        cleanCache("dish_"+dto.getCategoryId());

        return Result.success();
    }

    @GetMapping("/page")
    @Operation(summary = "分页查询", method = "GET")
    public Result<PageResult<DishVO>> page(DishPageQueryDTO dishPageQueryDTO) {
        log.info("菜品分页查询：{}", dishPageQueryDTO);
        PageResult<DishVO> pageResult = dishService.pageQuery(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    @DeleteMapping
    @Operation(summary = "批量删除", method = "DELETE")
    public Result<Void> delete(@RequestParam List<Long> ids) {
        log.info("需要删除的Ids：{}", ids);
        dishService.deleteBatch(ids);

        //批量删除有可能涉及好多Category，这里做粗暴处理，清理所有dish开头的缓存数据
        cleanCache("dish_*");

        return Result.success();
    }

    @GetMapping("/{id}")
    @Operation(summary = "按ID查询", method = "GET")
    public Result<DishVO> getById(@PathVariable Long id) {
        log.info("查询菜品的Id：{}", id);
        DishVO dishVO = dishService.getByIdWithFlavor(id);
        return Result.success(dishVO);
    }

    @PutMapping
    @Operation(summary = "编辑菜品", method = "PUT")
    public Result<Void> update(@RequestBody DishDTO dishDTO) {
        log.info("编辑的菜品：{}", dishDTO);
        dishService.updateWithFlavor(dishDTO);

        //编辑分类和菜品有可能涉及很多category的构成，这里做粗暴处理，清理所有dish开头的缓存数据
        cleanCache("dish_*");

        return Result.success();
    }

    @PostMapping("/status/{status}")
    @Operation(summary = "启停售卖", method = "POST")
    public Result<Void> startOrStop(@PathVariable Integer status, @RequestParam Long id) {
        log.info("需要开启/禁卖的Id：{}", id);
        dishService.startOrStop(status, id);

        //清理缓存数据
        Long categoryId = dishService.getCategoryId(id);
        cleanCache("dish_" + categoryId);
        return Result.success();
    }

    /**
     * 根据分类id查询菜品
     */
    @GetMapping("/list")
    @Operation(summary = "按分类id查询菜品")
    public Result<List<Dish>> list(Long categoryId) {
        List<Dish> list = dishService.list(categoryId);
        return Result.success(list);
    }

    private void cleanCache(String pattern) {
        Set<String> keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }

}
